<?php


namespace App\Web\Controller;


use App\Web\Facades\UserAuth;
use App\Web\Model\CaseFlow;
use App\Web\Model\CaseModel;
use EchoPHP\Http\Request;

class CaseTag extends Controller {

    /**
     * 获取客户的完整标签流程
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function getSubFlow(Request $request) {
        $subId = $request->getParam('subid/d', 0);
        $caseFlowModel = new CaseFlow();
        $flowList = $caseFlowModel->getSubFlowCode($subId);
        return $this->json([
            'status' => empty($flowList) ? false : true,
            'message' => empty($flowList) ? '该客户尚未定义流程标签' : '获取成功',
            'flowList' => $flowList
        ]);
    }

    /**
     * 获取项目的完整标签流程
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function getProjectFlowCode(Request $request) {
        $projectId = $request->getParam('projectid/d', 0);
        $caseFlowModel = new CaseFlow();
        $flowList = $caseFlowModel->getProjectFlowCode($projectId);

        $projectModel = new \App\Web\Model\Project();
        $contactFieldList = $projectModel->getContactFieldList($projectId);
        return $this->json([
            'status' => empty($flowList) ? false : true,
            'message' => empty($flowList) ? '该项目尚未定义流程标签' : '获取成功',
            'flowList' => $flowList,
            'contactfieldlist' => $contactFieldList['contactfieldlist']
        ]);
    }

    /**
     * 加入case获取职位列表
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function getActiveCase(Request $request) {
        $oprId = $this->getLoginId();
        $pageNum = $request->getParam('pagenum');
        $caseName = $request->getParam('casename');
        $projectName = $request->getParam('projectname');
        $caseModel = new CaseModel();
        list($pageNum, $rowFrom, $rowTo) = $caseModel->paginationParameters($pageNum);
        $count = $caseModel->getActiveCaseCount($oprId, $caseName, $projectName);
        $caseList = [];
        if ($count > 0) {
            $caseList = $caseModel->getActiveCaseList($oprId, $rowFrom, $rowTo, $caseName, $projectName);
        }
        return $this->json(['status' => true, 'message' => '获取成功', 'pageNum' => $pageNum,
            'count' => $count, 'caselist' => $caseList['caselist'] ?? [], 'projectlist' => $caseList['projectlist'] ?? [],]);
    }

    /**
     * 修改职位下简历标签状态时校验能够操作哪些一级标签
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     */
    public function checkFlowStatus(Request $request) {
        $projectId = $request->getParam('projectid/d', 0);
        $caseResumeIds = $request->getParam('caseresumeids', '');
        $caseResumeIds = explode(',', $caseResumeIds);
        $oprId = UserAuth::getLoginId();
        $caseModel = new CaseModel();
        $result = $caseModel->checkFlowStatus($oprId, $projectId, $caseResumeIds);
        return $this->json($result);
    }

    /**
     * 将简历加入职位中
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function addResumeToCase(Request $request) {
        $oprId = $this->getLoginId();  //获取操作人ID
        $caseId = $request->getPost('caseid/d', 0); //获取职位id
        $resumeIds = $request->getPost('resumeids', ''); //获取简历id
        $resumeIds = array_unique(explode(',', $resumeIds)); //过滤重复值
        $caseModel = new CaseModel(); //实例化简历模型
        $result = $caseModel->addToCase($oprId, $caseId, $resumeIds); //调用简历加入职位方法
        return $this->json($result); //返回最终结果
    }

    /**
     * 精英网简历加入职位中
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function addJyResumeToCase(Request $request) {
        $oprId = $this->getLoginId();
        $caseId = $request->getPost('caseid/d', 0);
        $managerIds = $request->getPost('managerids', '');
        $managerIds = formatIdsToArr($managerIds);
        $caseModel = new CaseModel();
        $result = $caseModel->addJyResumeToCase($oprId, $caseId, $managerIds);
        return $this->json($result);
    }

    /**
     * 添加标签，以步进方式添加标签，只能添加当前所在标记及下一级标签
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     * @throws \Throwable
     */
    public function addCaseTag(Request $request) {
        $oprId = $this->getLoginId();
        $subId = $request->getPost('subid', '');
        $caseResumeIds = $request->getPost('caseresumeids', '');
        $caseResumeIds = explode(',', $caseResumeIds);
        $tagCode = $request->getPost('tagcode', '');
        $timeControl = $request->getPost('timecontrol', '');
        $tagMemo = $request->getPost('tagmemo', '');
        $noTask = (bool)$request->getParam('notask', false);
        $salary = $request->getParam('salary', 0);
        $taskParams = $request->getPost('taskInfo');
        $taskParams = json_decode($taskParams, true) ?: [];
        if ($taskParams && isset($taskParams['receiverinfo'])) {
            $taskParams['receiverinfo'] = json_decode($taskParams['receiverinfo'], true);
        }
        $caseModel = new CaseModel();
        $result = $caseModel->addResumeTag($oprId, $subId, $caseResumeIds, $tagCode, $timeControl, $tagMemo, $taskParams, false, $noTask, $salary);
        return $this->json($result);
    }

    /**
     * 标签撤销
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function removeCaseTag(Request $request) {
        $oprId = UserAuth::getLoginId();
        $oprName = UserAuth::getLoginCname();
        $subId = $request->getPost('subid', '');
        $caseId = $request->getPost('caseid', '');
        $keyCode = $request->getPost('keycode', '');
        $tagHisId = $request->getPost('taghisid', '');

        $caseModel = new CaseModel();
        $result = $caseModel->removeResumeTag($oprId, $oprName, $subId, $caseId, $keyCode, $tagHisId);
        return $this->json($result);
    }

    public function getProjectsWithOpr(Request $request) {
        $subId = $request->getParam('subId/d');
        $oprId = $this->getLoginId();

        $caseModel = new CaseModel();
        $projectList = $caseModel->getProjectsWithOpr($subId, $oprId);
        if (empty($projectList)) {
            return $this->json(['status' => true, 'message' => '未找到记录', 'projectlist' => []]);
        }
        return $this->json(['status' => true, 'message' => '获取成功', 'projectlist' => $projectList]);
    }
}